import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/custom_widget.dart';
import 'package:osg/controllers/http.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../webview.dart';
import '../../web_tabs/details/settlement.dart';

class Settlement extends StatefulWidget {
  const Settlement({super.key});

  @override
  State<Settlement> createState() => _WebDetailState();
}
class _WebDetailState extends State<Settlement> {
  RxBool valgroup=true.obs;

  final thumb = Get.parameters['thumb'];
  final uuid = Get.parameters['uuid'];
  // ignore: non_constant_identifier_names
  final is_box = Get.parameters['is_box'];
  final title = Get.parameters['title'];
  final draw = Get.parameters['draw'];


  // ignore: non_constant_identifier_names
   final money_price = Get.parameters['money_price'];

 final allPrice =Rx<double>(0);


  RxInt num = 1.obs;

  RxMap payInfo={}.obs;

 

  getInfo(){
    Get.find<HTTPS>().getData('/wallet/walletStatus',{},'noload').then((res) {
      if (res['code']==11000) {
          PayWidget('需实名后才能开通钱包，现在去实名？','1');
      }else{
        payInfo.value = res;
       // PayWidget('需实名后才能开通钱包，现在去实名？');
      }
      print(res['code']);
    });
    
  }

    @override
    void initState() {
      super.initState();
      WidgetsBinding.instance.addPostFrameCallback((_) {
        getInfo();
    });
    }

  @override
  Widget build(BuildContext context) {


    return Scaffold(
        appBar: AppBar(
            backgroundColor: Color(0xffffffff),
            title: Text('请核对订单'),
            centerTitle: true),
        body: Padding(
          padding: EdgeInsets.symmetric(horizontal: 15),
          child: Column(
            children: [
              Expanded(
                  child: ListView(
                children: [
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 30,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                     
                      ClipRRect(
                        borderRadius: BorderRadius.circular(5.0), // 设置圆角大小
                        child: Image.network(
                          thumb!,
                          width: 80,
                          height: 80,
                          fit: BoxFit.fill,
                          errorBuilder: (context, error, stackTrace) {
                               return SizedBox(
                                width: 80,
                              );
                          }
                        ),
                      )
                      
                      ,
                      SizedBox(
                        width: 10,
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            title!,
                            style: TextStyle(
                                fontSize: 15, color: Color(0XFF0F172B)),
                          ),
                          // Text(
                          //   '限量1000份，流通100份',
                          //   style: TextStyle(
                          //       fontSize: 12, color: Color(0XFF999999)),
                          // ),
                          SizedBox(
                            height: 15,
                          ),
                          Container(
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(5),
                                border: Border.all(color: Color(0XFF1F0588))),
                            padding: EdgeInsets.symmetric(
                                horizontal: 5, vertical: 2),
                            child: Text(
                              is_box=='1'?'盲盒':'数字藏品',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0XFF1F0588)),
                            ),
                          )
                        ],
                      )
                      
                    ],
                  ),
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 30,
                  ),
                  Row(
                    children: [
                      Text(
                        '单价',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFF999999)),
                      ),
                      Expanded(child: SizedBox()),
                      Text(
                        '￥$money_price',
                        style: TextStyle(fontSize: 14),
                      )
                    ],
                  ),
                  if( draw!='1')

                  SizedBox(
                    height: 15,
                  ),
                                   if( draw!='1')

                  Row(
                    children: [
                      Text(
                        '数量',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFF999999)),
                      )
                      ,
                      Expanded(child: SizedBox()),
                      GestureDetector(
                        onTap: () => {
                          if(num.value>1){
                           num.value--,
                          allPrice.value = double.parse('$money_price') * num.value

                          }
                        },
                        child: Image.asset(
                          'static/web/jian.png',
                          width: 18,
                          fit: BoxFit.fitWidth,
                        ),
                      ),
                      SizedBox(
                        width: 20,
                      ),
                      Obx(()=>
                      Text(
                        '$num',
                        style: TextStyle(
                          fontSize: 16,
                        ),
                      )
                      )
                      ,
                      SizedBox(
                        width: 20,
                      ),
                       GestureDetector(
                        onTap: () => {
                           num.value++,

                          allPrice.value = double.parse('$money_price') * num.value

                        },
                        child: 
                      Image.asset(
                        'static/web/jia.png',
                        width: 18,
                        fit: BoxFit.fitWidth,
                      ),
                      ),
                      //
                    ],
                  ),

                                   if( draw!='1')

                  SizedBox(
                    height: 15,
                  ),
                  // Row(
                  //   children: [
                  //     Text(
                  //       '合并支付',
                  //       style:
                  //           TextStyle(fontSize: 14, color: Color(0XFF999999)),
                  //     ),
                  //     Expanded(child: SizedBox()),
                  //     Text(
                  //       '￥99.00',
                  //       style: TextStyle(fontSize: 15),
                  //     )
                  //   ],
                  // ),
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 50,
                  ),
                  Text(
                    '支付方式',
                    style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  Row(
                    children: [
                      // Expanded(
                      //   child: Container(
                      //     height: 37,
                      //     alignment: Alignment.center,
                      //     decoration: BoxDecoration(
                      //       borderRadius: BorderRadius.circular(5),
                      //       color: Color(0XFFF8F9FD),
                      //     ),
                      //     child: Text(
                      //       '易宝钱包',
                      //       style: TextStyle(color: Color(0XFF0F172B)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(
                      //   width: 10,
                      // ),
                      // Expanded(
                      //   child: Container(
                      //     height: 37,
                      //     alignment: Alignment.center,
                      //     decoration: BoxDecoration(
                      //       borderRadius: BorderRadius.circular(5),
                      //       color: Color(0XFFF8F9FD),
                      //     ),
                      //     child: Text(
                      //       '快付通',
                      //       style: TextStyle(color: Color(0XFF0F172B)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(
                      //   width: 10,
                      // ),
                      // Expanded(
                      //   child: Container(
                      //     width: 100,
                      //     height: 37,
                      //     alignment: Alignment.center,
                      //     decoration: BoxDecoration(
                      //       borderRadius: BorderRadius.circular(5),
                      //       color: Color(0XFFF8F9FD),
                      //     ),
                      //     child: Text(
                      //       '汇付天下',
                      //       style: TextStyle(color: Color(0XFF0F172B)),
                      //     ),
                      //   ),
                      // ),
                       Container(
                          width: 100,
                          height: 37,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(5),
                            color: Color(0XFF0F172B),
                          ),
                          child: Text(
                            '汇付天下',
                            style: TextStyle(color: Color(0XFFF8F9FD)),
                          ),
                        ),
                    ],
                  ),
                  Divider(
                    color: Color(0XFFDDDDDD),
                    height: 50,
                  ),
                  Text(
                    '说明',
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    '1、一个用户同时允许存在一笔待支付订单',
                    style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                  ),
                  Text(
                    '2、若您短时间未支付违约订单过多，将会被禁止购买一段时间',
                    style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Text(
                    '购买须知',
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    '数字藏品交易仅限于18周岁(不含)以上、60周岁(含)以下，并已完成实名认证的中国大陆用户购买。由于不同属性的数字艺术有不同的二次交易规则，请您仔细阅读并同意相关规则后再进行相应操作，以避免不必要的损失。 请注意，一旦数字藏品完成出售，不支持退货在购买之前，请仔细考虑您的购买决策。根据我国相关的法律要求，特定条件下的数字藏品的二次交易不支持任何形式的变相炒作。我们坚决反对数字藏品炒作、场外交易、欺诈，以及其他非法侵权行为。请您甄别上述涉嫌网络欺诈的行为并了解相关风险。',
                    style: TextStyle(fontSize: 12, color: Color(0XFF999999)),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                ],
              )),
              Align(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                   GestureDetector(
                onTap: (){
                    valgroup.value = !valgroup.value;
                },
                child:  Container(
                  width: 12,
                  height: 12,
                margin: EdgeInsets.only(top: 2,right: 3),
                child: 
                Obx(()=>
                Image.asset(valgroup.value?'static/web/shous.png':'static/web/shou.png',width: 12,height: 12, fit: BoxFit.fitWidth,)
                )
                
              ),
              ) ,

                    GestureDetector(
                onTap: (){
                    valgroup.value = !valgroup.value;
                },
                child:Text(
                      '我已阅读并同意《服务协议》',
                      style: TextStyle(fontSize: 12, color: Color(0XFF0F172B)),
                    )
                    )

                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.symmetric(vertical: 10),
                height: 60,
                decoration: BoxDecoration(
                    color: Color(0XFF0F172B),
                    borderRadius: BorderRadius.circular(60)),
                child: Row(
                  children: [
                    SizedBox(
                      width: 25,
                    ),
                    Text(
                      '￥',
                      style: TextStyle(fontSize: 16, color: Color(0XFFFFFFFF)),
                    ),
                    Obx(()=>
                      Text(
                        allPrice>0?'$allPrice':'$money_price',
                        style: TextStyle(fontSize: 24, color: Color(0XFFFFFFFF)),
                      )
                    )
                    ,
                    Expanded(child: SizedBox()),
                    GestureDetector(
                      onTap: ()
                          {
                            if(payInfo.isEmpty){
                              getInfo();
                            }else{
                              if(!payInfo['HF']){

                                 PayWidget('您还未开通此钱包, 是否前往开通？','2');
                            }else{
                                Get.find<HTTPS>().postData('/products/orders', {
                                  'uuid':uuid,
                                  'total':'$num',
                                  'url':"back.html?web=back",
                                  'wallet':"HF"
                                }).then((res){
                                  if(res!=null){
                                    if(kIsWeb) {
              final uur=  Uri.parse('${res['order']['pay_url']}');
              launchUrl(uur,webOnlyWindowName: '_self',);
          }else{

                                     Get.toNamed('/webview?url=${res['order']['pay_url']}&back=suu&id=${res['order']['id']}');
                                  }
          }
                                  
                                });
                            }
                            }

                            

                            // Get.toNamed('/web_tabs/details/pay_success')
                            },
                      child: Text(
                        '立即支付',
                        style:
                            TextStyle(fontSize: 18, color: Color(0XFFFFFFFF)),
                      ),
                    ),
                    SizedBox(
                      width: 25,
                    ),
                  ],
                ),
              )
            ],
          ),
        ));
  }
}
